package com.wbs.shop.nuby.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/1/6 0006.
 */

public class SearchEditText extends InputEditText implements TextView.OnEditorActionListener {
    public static final long DEFAUFT_TIME_OUT = 1000;
    long timeout = DEFAUFT_TIME_OUT;
    boolean isFilst = true;

    TextWatcher watcher;

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SearchEditText(Context context) {
        super(context);
        init();
    }

    private void init() {
        setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        setSingleLine();
        setMaxLines(1);
        setOnEditorActionListener(this);
    }

    @Override
    public void afterTextChanged(Editable s) {
        //文字变动 ， 有未发出的搜索请求，应取消
        if (mHandler.hasMessages(MSG_SEARCH)) {
            mHandler.removeMessages(MSG_SEARCH);
        }
        mHandler.sendEmptyMessageDelayed(MSG_SEARCH, timeout); //自动搜索功能 删除
        //如果为空 直接显示搜索历史
//        if (TextUtils.isEmpty(s)) {
//            Logger.getInstance().e("空值");
//            watcher.afterTextChanged(s);
//        } else {//否则延迟500ms开始搜索
//            mHandler.sendEmptyMessageDelayed(MSG_SEARCH, timeout); //自动搜索功能 删除
//        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        super.onTextChanged(s, start, count, after);
        if (watcher != null) {
            watcher.onTextChanged(s, start, count, after);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        super.beforeTextChanged(s, start, count, after);
        if (watcher != null) {
            watcher.beforeTextChanged(s, start, count, after);
        }
    }

    @Override
    public void addTextChangedListener(TextWatcher watcher) {
        if (watcher == this) {
            //super.addTextChangedListener(watcher);
        } else {
            this.watcher = watcher;
        }
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (watcher != null) {
                if (isFilst) {
                    isFilst = false;
                    return;
                }
                watcher.afterTextChanged(getText());
            }
        }
    };
    private static final int MSG_SEARCH = 1;

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH && watcher != null) {
            watcher.afterTextChanged(getText());
        }
        return false;
    }

    @Override
    public void clearFocus() {
        super.clearFocus();
        hideKeyboard(this);
    }

    public void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
